package org.feifei.org.service.impl;

import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.service.BaseService;
import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.basic.util.PageList;
import org.feifei.org.domain.Shop;
import org.feifei.org.mapper.EmployeeMapper;
import org.feifei.org.mapper.ShopMapper;
import org.feifei.org.query.ShopQuery;
import org.feifei.org.service.EmployeeService;
import org.feifei.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ShopService 的实现类
 */
@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private EmployeeService employeeService;

    @Resource
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void settlement(Shop shop) {
        // 基本信息审核，先判断当前店铺是否已经入住过
        Shop shopDB = shopMapper.findByAddressAndName(shop);
        if (shopDB != null) throw new BusinessException("该店铺已经注册过了，无法重复注册！");
        // a.店铺入住，首先应该 保存employee信息，获取自增长的id
        employeeService.save(shop.getAdmin());
        // b.然后在保存在shop的信息中，同时获取shop的自增长id
        shopMapper.save(shop);
        // c.最后更新employee中的shop_id
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }
}
